|
Inheritance (OOP) is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. Inheritance was invented in 1967 for Simula. Inheritance should not be confused with subtyping. In some languages inheritance and subtyping agree, while in others they differ; in general subtyping establishes an is-a relationship, while inheritance only reuses implementation and establishes a syntactic relationship, not necessarily a semantic relationship (inheritance does not ensure behavioral subtyping). To distinguish these concepts, subtyping is also known as ''interface inheritance,'' while inheritance as defined here is known as ''implementation inheritance'' or ''code inheritance''. Still, inheritance is a commonly used mechanism for establishing subtype relationships. Inheritance is contrasted with object composition, where one object ''contains'' another object (or objects of one class contain objects of another class); see composition over inheritance. Composition implements a has-a relationship, in contrast to the is-a relationship of subtyping. ==Types of inheritance== There are various types of inheritance, based on paradigm and specific language. ;Single inheritance: where subclasses inherit the features of one super class. A class acquires the properties of another class. ;Multiple inheritance: where one class can have more than one super class and inherit features from all parent classes. ;Multilevel inheritance: where a subclass is inherited from another subclass. It is not uncommon that a class is derived from another derived class as shown in the figure "Multilevel inheritance". The class ''A'' serves as a ''base class'' for the ''derived class'' ''B'', which in turn serves as a ''base class'' for the ''derived class'' ''C''. The class ''B'' is known as ''intermediate'' base class since it provides a link for the inheritance between ''A'' and ''C''. The chain ''ABC'' is known as ''inheritance path''. A derived class with multilevel inheritance is declared as follows: Class A(...); //Base class Class B : public A(...); //B derived from A Class C : public B(...); //C derived from B This process can be extended to any number of levels. ;Hierarchical inheritance: where one class serves as a superclass (base class) for more than one sub class. ;Hybrid inheritance: a mix of two or more of the above types of inheritance. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Inheritance (object-oriented programming)」の詳細全文を読む スポンサード リンク
|